package com.jspgou.cms.manager.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jspgou.common.hibernate3.Updater;
import com.jspgou.common.page.Pagination;
import com.jspgou.cms.dao.StoreModelDao;
import com.jspgou.cms.entity.StoreModel;
import com.jspgou.cms.manager.StoreModelMng;

@Service
@Transactional
public class StoreModelMngImpl implements StoreModelMng {
	@Transactional(readOnly = true)
	public Pagination getPage(int pageNo, int pageSize) {
		Pagination page = dao.getPage(pageNo, pageSize);
		return page;
	}

	@Transactional(readOnly = true)
	public StoreModel findById(Long id) {
		StoreModel entity = dao.findById(id);
		return entity;
	}
	
	public List<StoreModel> getList(){
		return dao.getList();
	}
	
	public StoreModel getByDefaultModel(Boolean defaultModel){
		return dao.getByDefaultModel(defaultModel);
	}

	public StoreModel save(StoreModel bean) {
		dao.save(bean);
		return bean;
	}

	public StoreModel update(StoreModel bean) {
		Updater<StoreModel> updater = new Updater<StoreModel>(bean);
		StoreModel entity = dao.updateByUpdater(updater);
		return entity;
	}
	
	public StoreModel[] updateStoreModels(Long[] ids,Boolean[] defaultModels){
		StoreModel[] beans = new StoreModel[ids.length];
		for (int i = 0, len = ids.length; i < len; i++) {
			beans[i] = findById(ids[i]);
			beans[i].setDefaultModel(defaultModels[i]);
		}
		return beans;
	}

	public StoreModel deleteById(Long id) {
		StoreModel bean = dao.deleteById(id);
		return bean;
	}
	
	public StoreModel[] deleteByIds(Long[] ids) {
		StoreModel[] beans = new StoreModel[ids.length];
		for (int i = 0,len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}

	private StoreModelDao dao;

	@Autowired
	public void setDao(StoreModelDao dao) {
		this.dao = dao;
	}
}